//
//  LotGDActionPickerViewController.m
//  LotGD
//
//  Created by Thiago on 20/07/2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "ActionPickerViewController.h"
#import "NavBarViewController.h"
#import "NavBarAppDelegate.h"

@implementation ActionPickerViewController
@synthesize actionPicker, pickerData, pickerLinks, rootView, currentPickerIndex, pickerToolbar;

- (UIView *)returnView {
	[self loadView];
	return rootView;
}	

- (void) dropView {
	// Remove the picker and its toolbar from view
	NavBarAppDelegate *delegate = (NavBarAppDelegate *) [ [ UIApplication sharedApplication] delegate];
	delegate.isAVLoaded = 0;
	[actionPicker removeFromSuperview];
	[pickerToolbar removeFromSuperview];
	[pickerData release];
}


// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	
	//Set up action picker view
	actionPicker = [[UIPickerView alloc] init];
	
	CGRect currentWindow = [[UIScreen mainScreen] bounds];
	CGRect actionPickerBounds = [actionPicker bounds];
	CGRect rectArea = CGRectMake(0, (actionPickerBounds.size.height+30), (currentWindow.size.width), (actionPickerBounds.size.height) );
	
	[actionPicker setFrame:rectArea];
	actionPicker.showsSelectionIndicator = YES;
	actionPicker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
	actionPicker.delegate = self;
	actionPicker.dataSource = self;
	actionPicker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
	
	//Set delegate
	NavBarAppDelegate *delegate = (NavBarAppDelegate *) [ [ UIApplication sharedApplication] delegate];
	rootView = [delegate myWebView];
	
	//Set up action array content along with the appropriate URLs
	if ( [delegate.actionDescrArray count] > 0 ) {
		// Fill pickerview with actions scraped from page
		self.pickerData = [delegate.actionDescrArray copy ];
		pickerLinks = [delegate.actionURLArray copy];
	} else {
		// Empty action array, so we'll add a default
		self.pickerData = [[NSMutableArray alloc] initWithObjects:@"Continue", nil];
		pickerLinks = [[NSMutableArray alloc] initWithObjects:[delegate.userPrefences objectAtIndex:0], nil];
	}	
	
	// Create the toolbar above the action picker
	pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, (actionPickerBounds.size.height-14), (currentWindow.size.width), 44)];
	
	pickerToolbar.barStyle = UIBarStyleBlackOpaque;
	[pickerToolbar sizeToFit];
	
	// Create items for the action picker toolbar
	NSMutableArray *barItems = [[NSMutableArray alloc] init];
	
	UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonSystemItemCancel
															   target:self action:@selector(dropView)];	
	[barItems addObject:cancelBtn];
	
	UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
	[barItems addObject:flexSpace];
	
	UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"  Do  " style:UIBarButtonItemStyleDone
															   target:self action:@selector(doButtonAction:)];
	[barItems addObject:doneBtn];
	
	// Add the items to toolbar 
	[pickerToolbar setItems:barItems];

	// Add the pickerview and its toolbar on top of the existing view
	[rootView addSubview:pickerToolbar];
	[rootView addSubview:actionPicker];
	[actionPicker release];
	[pickerToolbar release];
	
}

-(void) doButtonAction:(id) sender {
	NavBarAppDelegate *delegate = (NavBarAppDelegate *) [ [ UIApplication sharedApplication] delegate];
	
	NSString *tempString = [pickerData objectAtIndex:currentPickerIndex];
	// If the action item starts with LoGD, assume external link and use Safari to open
	if( ([tempString length] > 3) && ([[tempString substringToIndex:4] isEqualToString:@"LoGD"]) ) {
		[self askToOpenSafari];
	} else {
			[self dropView];
		if([self.pickerData count] != 0) {
			[delegate.mainController processURL:[NSURL URLWithString:[pickerLinks objectAtIndex:currentPickerIndex]]];
			[pickerLinks release];
		}
	}
	
	[tempString release];

}	


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

#pragma mark -
#pragma mark Picker Data Source Methods
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
	return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
	return [pickerData count];	
}

#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView
			 titleForRow:(NSInteger)row
			forComponent:(NSInteger)component {
				return [pickerData objectAtIndex:row];
}

-(void)pickerView:(UIPickerView *)pickerView 
	 didSelectRow:(NSInteger)row 
	  inComponent:(NSInteger)component {
		currentPickerIndex = row;
}

-(void)askToOpenSafari {
	// Open an alertview alerting the user to the fact that Safari will open. User can then cancel or proceed.
	NSString *alertText = [[NSString alloc] initWithString:@"This link will open Safari"];
	UIAlertView *alert = [ [UIAlertView alloc] initWithTitle:@"External Link" message:alertText delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Proceed", nil];
	[alert show];
	[alert release];
	[alertText release];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
	if ( buttonIndex == 0) { }
	else {
		[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[pickerLinks objectAtIndex:currentPickerIndex]]];
	}	
}



- (void)dealloc {
	[actionPicker release];
	[pickerData release];
	[pickerLinks release];
	[pickerToolbar release];
	[rootView release];
    [super dealloc];
}


@end
